home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / 3dlib30a.zip / CTM3D.INT < prev    next >
Text File  |  1994-03-10  |  10KB  |  167 lines

  1. (******************************************************************************
  2. *                                    Ctm3d                                    *
  3. ******************************************************************************)
  4. Unit Ctm3d;
  5.  
  6. (*******************************************************************************
  7. *                          Homogeneous Coordinates                            *
  8. *                          -----------------------                            *
  9. *                                                                             *
  10. *      Homogeneous coordinates allow transformations to be represented by     *
  11. *      matrices. A 3x3 matrix is used for 2D transformations, and a 4x4 matrix*
  12. *      for 3D transformations.                                                *
  13. *                                                                             *
  14. *      THIS MODULE IMPLEMENTS ONLY 3D TRANSFORMATIONS.                        *
  15. *                                                                             *
  16. *      in homogeneous coordination the point P(x,y,z) is represented as       *
  17. *      P(w*x, w*y, w*z, w) for any scale factor w!=0.                         *
  18. *      in this module w == 1.                                                 *
  19. *                                                                             *
  20. * Transformations:                                                            *
  21. *          1. translation                                                     *
  22. *                  [x, y, z] --> [x + Dx, y + Dy, z + Dz]                     *
  23. *                                                                             *
  24. *                              ┌          ┐                                   *
  25. *                              │1  0  0  0│                                   *
  26. *              T(Dx, Dy, Dz) = │0  1  0  0│                                   *
  27. *                              │0  0  1  0│                                   *
  28. *                              │Dx Dy Dz 1│                                   *
  29. *                              └          ┘                                   *
  30. *          2. scaling                                                         *
  31. *                  [x, y, z] --> [Sx * x, Sy * y, Sz * z]                     *
  32. *                                                                             *
  33. *                          ┌          ┐                                       *
  34. *                          │Sx 0  0  0│                                       *
  35. *              S(Sx, Sy) = │0  Sy 0  0│                                       *
  36. *                          │0  0  Sz 0│                                       *
  37. *                          │0  0  0  1│                                       *
  38. *                          └          ┘                                       *
  39. *                                                                             *
  40. *          3. rotation                                                        *
  41. *                                                                             *
  42. *              a) Around the Z axis:                                          *
  43. *                                                                             *
  44. *                  [x, y, z] --> [x*cost - t*sint, x*sint + y*cost, z]        *
  45. *                      ┌                  ┐                                   *
  46. *                      │cost  sint   0   0│                                   *
  47. *              Rz(t) = │-sint cost   0   0│                                   *
  48. *                      │0     0      1   0│                                   *
  49. *                      │0     0      0   1│                                   *
  50. *                      └                  ┘                                   *
  51. *                                                                             *
  52. *              b) Around the X axis:                                          *
  53. *                                                                             *
  54. *                  [x, y, z] --> [x, y*cost - z*sint, y*sint + z*cost]        *
  55. *                      ┌                  ┐                                   *
  56. *                      │1     0     0    0│                                   *
  57. *              Rx(t) = │0     cost  sint 0│                                   *
  58. *                      │0    -sint  cost 0│                                   *
  59. *                      │0     0     0    1│                                   *
  60. *                      └                  ┘                                   *
  61. *                                                                             *
  62. *              c) Around the Y axis:                                          *
  63. *                                                                             *
  64. *                  [x, y, z] --> [xcost + z*sint, y, z*cost - x*sint]         *
  65. *                      ┌                  ┐                                   *
  66. *                      │cost  0   -sint  0│                                   *
  67. *              Ry(t) = │0     1    0     0│                                   *
  68. *                      │sint  0    cost  0│                                   *
  69. *                      │0     0    0     1│                                   *
  70. *                      └                  ┘                                   *
  71. *                                                                             *
  72. *   transformation of the vector [x,y,z,1] by transformation matrix T is given *
  73. *   by the formula:                                                           *
  74. *                                         ┌   ┐                               *
  75. *              [x', y', z', 1] = [x,y,z,1]│ T │                               *
  76. *                                         └   ┘                               *
  77. * Optimizations:                                                              *
  78. *   The most general composition of R, S and T operations will produce a matrix*
  79. *   of the form:                                                              *
  80. *              ┌                       ┐                                      *
  81. *              │r11    r12     r13    0│                                      *
  82. *              │r21    r22     r23    0│                                      *
  83. *              │r31    r32     r33    0│                                      *
  84. *              │tx     ty      tz     1│                                      *
  85. *              └                       ┘                                      *
  86. *   The task of matrix multiplication can be simplified by                    *
  87. *      x' = x*r11 + y*r21 + z*r31 + tx                                        *
  88. *      y' = x*r12 + y*r22 + z*r32 + ty                                        *
  89. *      z' = x*r13 + y*r23 + z*r33 + tz                                        *
  90. *                                                                             *
  91. *                                                                             *
  92. * See also:                                                                   *
  93. *      "Fundamentals of Interactive Computer Graphics" J.D FOLEY A.VAN DAM    *
  94. *      Adison-Weslely ISBN 0-201-14468-9 pp 245-265                           *
  95. *******************************************************************************)
  96.  
  97. interface
  98.  
  99. uses 
  100.    hdr3d
  101.    ;
  102.  
  103. type
  104.     ctmPtr = ^ ctm;         
  105.     ctm = object
  106.        r11, r12, r13   : real; { change to single if numeric processor is present }
  107.        r21, r22, r23   : real;
  108.        r31, r32, r33   : real;
  109.        tx,  ty,  tz    : real;
  110.  
  111.        constructor SetUnit; { set to the unit (I) matrix }
  112.        constructor Copy(var src : ctm); { construct from another }
  113.        procedure save(var dest : ctm);
  114.  
  115.        procedure translate(Dx, Dy, Dz : real); { used to move .. }
  116.  
  117.        procedure translateX(dx : real);
  118.        procedure translateY(dy : real);
  119.        procedure translateZ(dz : real); {